package com.data.service.report.impl;

import com.data.domain.report.*;
import com.data.mapper.report.BatchpmtinfoMapper;
import com.data.mapper.report.BatchpmtinfoMapper;
import com.data.mapper.report.PrtryinfoMapper;
import com.data.mapper.report.ext.BatchpmtinfoExtMapper;
import com.data.model.ReportModel;
import com.data.service.report.BatchpmtinfoService;
import com.data.service.report.BatchpmtinfoService;
import com.data.utill.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "batchpmtinfoService")
public class BatchpmtinfoServiceImpl implements BatchpmtinfoService {

    @Autowired
    private BatchpmtinfoMapper batchpmtinfoMapper;

    @Autowired
    private PrtryinfoMapper prtryinfoMapper;

    @Autowired
    private BatchpmtinfoExtMapper batchpmtinfoExtMapper;

    @Override
    public List<Batchpmtinfo> getBatchpmtinfo(ReportModel reportModel) {
        BatchpmtinfoCriteria batchpmtinfoCriteria=new BatchpmtinfoCriteria();
        BatchpmtinfoCriteria.Criteria criteria=batchpmtinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //业务类型
        if (StringUtils.isNoNullString(reportModel.getPrtry())){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }
        //发起机构
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getSender()))){
            criteria.andSenderEqualTo(StringUtils.getIdFromString(reportModel.getSender()));
        }
        batchpmtinfoCriteria.setOrderByClause("workdate desc");
        List<Batchpmtinfo> data = new ArrayList<>();
        RowBounds rowBounds = new RowBounds();
        if (StringUtils.isNoNullString(reportModel.getStart()) && StringUtils.isNoNullString(reportModel.getLength())){
            rowBounds = new RowBounds(Integer.parseInt(reportModel.getStart()),Integer.parseInt(reportModel.getLength()));
        }
        for (Batchpmtinfo batchpmtinfo:batchpmtinfoMapper.selectByExampleWithRowbounds(batchpmtinfoCriteria,rowBounds)){
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(batchpmtinfo.getPrtry());
            batchpmtinfo.setPrtry(prtryinfoMapper.selectByExample(prtryinfoCriteria).get(0).getName());
            data.add(batchpmtinfo);
        }
        return data;
    }
    /**
     * 基本的统计
     * @param reportModel
     * @return
     */
    @Override
    public int countBatchpmtinfo(ReportModel reportModel){
        BatchpmtinfoCriteria batchpmtinfoCriteria = new BatchpmtinfoCriteria();
        BatchpmtinfoCriteria.Criteria criteria=batchpmtinfoCriteria.createCriteria();
        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //业务类型
        if (StringUtils.isNoNullString(reportModel.getPrtry())){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }
        //发起机构
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getSender()))){
            criteria.andSenderEqualTo(StringUtils.getIdFromString(reportModel.getSender()));
        }
        return batchpmtinfoMapper.countByExample(batchpmtinfoCriteria);
    }

    /**
     * 在线流水物理分页查询
     * @param
     * @return
     */
    @Override
    public List<Batchpmtinfo> selectBatchpmtinfoByPage(ReportModel reportModel){
        String begin=StringUtils.cutDateStr(reportModel.getBegin());
        String senddate=StringUtils.cutDateStr(reportModel.getSenddate());
        String prtry=StringUtils.getIdFromString(reportModel.getPrtry());
        String sender=StringUtils.getIdFromString(reportModel.getSender());
        List<Batchpmtinfo> data = new ArrayList<>();
        List<Batchpmtinfo> batchpmtinfos = batchpmtinfoExtMapper.selectBatchpmtinfo(begin,reportModel.getEnd(),reportModel.getRefid(),prtry,
                senddate,reportModel.getErrcode(),sender,reportModel.getStart(),reportModel.getLength());
        for (Batchpmtinfo batchpmtinfo:batchpmtinfos) {
            String ptrcode = batchpmtinfo.getPrtry();
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(ptrcode);
            List<Prtryinfo> prtryinfos = prtryinfoMapper.selectByExample(prtryinfoCriteria);
            if (null!=prtryinfos&&prtryinfos.size()>0){
                batchpmtinfo.setPrtry(prtryinfos.get(0).getName());
            }
            data.add(batchpmtinfo);
        }
        return data;
    }

    /**
     * 用附加批次号取的批量业务收付款明细
     * @param sendbatchno
     * @return
     */
    public Batchpmtinfo getBatchpmtinfoByAppendBatNo(String sendbatchno){
        BatchpmtinfoCriteria batchpmtinfoCriteria=new BatchpmtinfoCriteria();
        BatchpmtinfoCriteria.Criteria criteria=batchpmtinfoCriteria.createCriteria();
        List<Batchpmtinfo> list=batchpmtinfoMapper.selectByExample(batchpmtinfoCriteria);
        if(list.size()>=1){
            return list.get(0);
        }else {
            return null;
        }
    }




}
